package com.naihe.anderson.sms.widgets;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.naihe.anderson.sms.R;

public class SystemAlertDialog extends Dialog {

	private Handler mHandler;
	
	TextView titleText;
	TextView contentTv;
	Button checkbtnOk;
	Button checkbtnCancel;
	
	String title = "";
	String content = "";
	String checkbtnOkText = "";
	String checkbtnCancelText = "";
	Message message;
	
	public SystemAlertDialog(Context context, Handler mHandler, String title, String content, String checkbtnCancelText ,String checkbtnOkText, Message message) {
		super(context, R.style.progressStyle);
		this.mHandler = mHandler;
		this.title = title;
		this.content = content;
		this.message = message;
		
		this.checkbtnCancelText = checkbtnCancelText;
		this.checkbtnOkText = checkbtnOkText;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.system_alert_layout);
		
		titleText = (TextView) findViewById(R.id.title_text);
		if(TextUtils.isEmpty(title)){
			titleText.setVisibility(View.GONE);
		} else {
			titleText.setText(title);
		}
		contentTv = (TextView) findViewById(R.id.content_tv);
		contentTv.setText(content);
		contentTv.setMovementMethod(ScrollingMovementMethod.getInstance());		
		
		checkbtnOk = (Button) findViewById(R.id.checkbtn_ok);
		if(TextUtils.isEmpty(checkbtnOkText)){
			checkbtnOk.setVisibility(View.GONE);
		} else {
			checkbtnOk.setText(checkbtnOkText);
		}
		checkbtnOk.setOnClickListener(new Button.OnClickListener(){  
			  
            public void onClick(View v) {  
            	checkbtnOk.setClickable(false);//保存只能点一次
                if (mHandler != null && message != null){
                	mHandler.sendMessage(message);
                }
                SystemAlertDialog.this.cancel();
                SystemAlertDialog.this.dismiss();
            }  
        });
		checkbtnCancel = (Button) findViewById(R.id.checkbtn_cancel);
		if(TextUtils.isEmpty(checkbtnCancelText)){
			checkbtnCancel.setVisibility(View.GONE);
		} else {
			checkbtnCancel.setText(checkbtnCancelText);
		}
		checkbtnCancel.setOnClickListener(new Button.OnClickListener(){  
			  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
            	SystemAlertDialog.this.cancel();
            	SystemAlertDialog.this.dismiss();
            }  
        });
	}
	
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			this.cancel();
			mHandler = null;
			this.dismiss();
		}
		return super.onKeyDown(keyCode, event);

	}

	@Override
	public void show() {
		super.show();
	}

}
